﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web;
using Framework;

namespace Enterprise
{
    public class ArticleImageService : BaseService
    {
        public string[] Add(int articleId, bool isCover, HttpPostedFileBase filedata)
        {

            var uploadHelper = new UploadHelper
            {
                PostedFile = filedata,
                RelativePath = "~/Upload/Article/",
                IsAdaptive = true,
                IsImage = true,
                IsGenerateThumbnail = true,
                FileMaxLength = 1024 * 1024 * 2,
                ThumbnailHeight = 185,
                ThumbnailWidth = 165,
                ThumbnailPrefix = "thum"
            };
            uploadHelper.Save();
            var fileName = uploadHelper.FileName;
            var thumFileName = uploadHelper.ThumbnailFileName;
            var sb = new StringBuilder();
            sb.Append("insert into tbArticleImage(");
            sb.Append("CoArticleId,CoIsCover,CoNormalImage,CoSmallImage,CoBigImage,CoName,CoDescription");
            sb.Append(") values(");
            sb.Append("@CoArticleId,@CoIsCover,@CoNormalImage,@CoSmallImage,@CoBigImage,@CoName,@CoDescription");
            sb.Append(")");
            var parameter = new[]
                                {
                                    new SqlParameter("@CoArticleId", SqlDbType.Int),
                                    new SqlParameter("@CoIsCover",SqlDbType.Bit),
                                    new SqlParameter("@CoNormalImage", SqlDbType.VarChar,255),
                                    new SqlParameter("@CoSmallImage", SqlDbType.VarChar,255),
                                    new SqlParameter("@CoBigImage", SqlDbType.VarChar,255),
                                    new SqlParameter("@CoName", SqlDbType.VarChar,255),
                                    new SqlParameter("@CoDescription", SqlDbType.VarChar)
                                };
            if (articleId == 0)
            {
                parameter[0].Value = DBNull.Value;
            }
            else
            {
                parameter[0].Value = articleId;
            }
            parameter[1].Value = isCover;
            parameter[2].Value = fileName;
            parameter[3].Value = thumFileName;
            parameter[4].Value = fileName;
            parameter[5].Value = "";
            parameter[6].Value = "";
            var rows = DbHelperSql.ExecuteSql(sb.ToString(), parameter).SafeToInt();
            if (rows > 0)
            {
                return new[] { rows.ToString(CultureInfo.InvariantCulture), fileName, thumFileName };
            }
            throw new Exception("添加失败！");
        }
        /// <summary>
        /// 删除文章图片
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            var path = HttpContext.Current.Server.MapPath("~/Upload/Article/");
            var ds = DbHelperSql.Query("select * from tbArticleimage where coArticleimageid={0}".FormatWith(id));
            foreach (DataRow item in ds.Tables["ds"].Rows)
            {
                FileHelper.DeleteSub(Path.Combine(path, item["CoNormalImage"].SafeToString()));
                FileHelper.DeleteSub(Path.Combine(path, item["CoSmallImage"].SafeToString()));
                FileHelper.DeleteSub(Path.Combine(path, item["CoBigImage"].SafeToString()));
            }
            int rows = DbHelperSql.ExecuteSql("delete  from tbArticleimage where coArticleimageid={0}".FormatWith(id));
            if (!(rows > 0))
                throw new Exception("记录不存在！");


        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public Table GetTable(int id)
        {
            var table = new Table
              {
                  Rows = DbHelperSql.Query("select * from TbArticleImage where CoArticleId={0}".FormatWith(id)).Tables["ds"].Rows,
                  Total = DbHelperSql.GetSingle("select count(*) from TbArticleImage where CoArticleId={0}".FormatWith(id)).SafeToInt()
              };
            return table;
        }
        /// <summary>
        /// 根据ID获取一条记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataRow GetById(int id)
        {
            return DbHelperSql.Query("select * from TbArticleImage where CoArticleId={0}".FormatWith(id)).Tables["ds"].Rows[0];
        }

    }
}
